﻿using System;
using System.Collections.Generic;
using System.Text;
using Wjtour.Common;
using Wjtour.Domain;
using System.Linq;
using System.Data.Common;

namespace Wjtour.BLL
{
    public class webrole_bll : base_dll
    {
        public PaginationData<Wjtour.Domain.webrole> GetList(string q, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(q))
                where += " and nick like '%" + Utility.GetSafeSql(q) + "%' ";
            string sql = string.Format(@"select * from webrole 
                {0} 
                order by id desc", where);
            PaginationData<Wjtour.Domain.webrole> data = GetPaginationData<Wjtour.Domain.webrole>(sql, page, pagesize);
            return data;
        }

        public Wjtour.Domain.webrole Load(int id)
        {
            string sql = "select * from webrole where id=" + id;
            return DbAccess.GetObjects<Wjtour.Domain.webrole>(sql).FirstOrDefault();
        }

        public bool Save(Wjtour.Domain.webrole webrole)
        {
            string sql = "";
            if (webrole.id > 0)
            {
                sql = string.Format(@"update webrole 
                set title='{0}',url='{1}',img='{2}',type={3},sort={4}
                where id={5}",
                             webrole.id);
            }
            else
                sql = string.Format(@"insert into link(nick) values('{0}')", webrole.id);

            return DbAccess.ExecuteNonQuery(sql) > 0;
        }

        public bool UpdateRights(int roleid, string roletitle, string[] menuids)
        {
            try
            {
                DbAccess.BeginTrans();
                DbCommand cmd = DbAccess.CreateCommand();
                if (roleid > 0)
                    cmd.CommandText = "update webrole set title='" + Utility.GetSafeSql(roletitle) + "' where id=" + roleid;
                else
                    cmd.CommandText = "insert into webrole(title,status,[sys]) values('" + Utility.GetSafeSql(roletitle) + "',1,0)";
                cmd.CommandType = System.Data.CommandType.Text;
                DbAccess.ExecuteNonQuery(cmd);

                cmd = DbAccess.CreateCommand();
                cmd.CommandText = "delete from menu_access where role_id=" + roleid;
                cmd.CommandType = System.Data.CommandType.Text;
                DbAccess.ExecuteNonQuery(cmd);

                foreach (var menuid in menuids)
                {
                    cmd = DbAccess.CreateCommand();
                    cmd.CommandText = "insert into menu_access(role_id,menu_id) values(" + roleid + "," + menuid + ")";
                    cmd.CommandType = System.Data.CommandType.Text;
                    DbAccess.ExecuteNonQuery(cmd);
                }
                DbAccess.CommintTrans();
                return true;
            }
            catch (Exception ex)
            {
                DbAccess.RollbackTrans();
                return false;
            }
        }

        public int BatchDelete(string ids)
        {
            string sql = "delete from webrole where id in(" + ids + ")";
            return DbAccess.ExecuteNonQuery(sql);
        }
    }
}
